package me.jessyan.mvparms.demo.mvp.ui.activity.integral;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;
import com.jess.arms.widget.CustomPopupWindow;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.integral.DaggerGiftDetailsComponent;
import me.jessyan.mvparms.demo.di.module.integral.GiftDetailsModule;
import me.jessyan.mvparms.demo.mvp.contract.integral.GiftDetailsContract;
import me.jessyan.mvparms.demo.mvp.presenter.integral.GiftDetailsPresenter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomIntColorChangeTextView;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;
import me.jessyan.mvparms.demo.mvp.ui.widget.banner.BannerViewPager;
import me.jessyan.mvparms.demo.mvp.ui.widget.banner.PageBean;
import me.jessyan.mvparms.demo.mvp.ui.widget.banner.PageHelpterListener;
import me.jessyan.mvparms.demo.mvp.ui.widget.banner.ZoomIndicator;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class GiftDetailsActivity extends BaseActivity<GiftDetailsPresenter> implements GiftDetailsContract.View ,PopupWindow.OnDismissListener{
    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.viewpager)
    BannerViewPager viewpager;
    @BindView(R.id.user_score_tv)
    CustomIntColorChangeTextView userScoreTv;
    @BindView(R.id.get_tv)
    TextView getTv;
    @BindView(R.id.indicator)
    ZoomIndicator indicator;
    @Inject
    @Named("deletePop")
    CustomPopupWindow deletePop;
    @Inject
    @Named("sureDeletePop")
    CustomPopupWindow sureDeletePop;
    @Inject
    @Named("confirmChangePop")
    CustomPopupWindow confirmChangePop;
    @Inject
    @Named("successChangePop")
    CustomPopupWindow successChangePop;

    private int type;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        type = getIntent().getIntExtra("type", -1);
        DaggerGiftDetailsComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .giftDetailsModule(new GiftDetailsModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_gift_details; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initToolbar();
        getTv.setVisibility(View.VISIBLE);
        initBanner();
        initPop();
    }

    private void initPop() {
        if (sureDeletePop != null  ) {
            sureDeletePop.setOnDismissListener(this);
        }

        if(confirmChangePop!=null ){
            confirmChangePop.setOnDismissListener(this);
        }
        if(successChangePop!=null ){
            successChangePop.setOnDismissListener(this);
        }
    }

    private void initBanner() {

        List<Integer> icons = new ArrayList<>();
        icons.add(R.mipmap.beauty1);
        icons.add(R.mipmap.beauty2);
        icons.add(R.mipmap.beauty3);
        icons.add(R.mipmap.beauty4);

        PageBean bean = new PageBean.Builder<Integer>()
                .setDataObjects(icons)
                .setIndicator(null)
                .builder();


        viewpager.setPageListener(bean, R.layout.item_gift_detail_banner, new PageHelpterListener<Integer>() {
            @Override
            public void getItemView(View view, Integer data) {
                ImageView imageView = (ImageView) view;
                imageView.setBackgroundResource(data);
            }
        });
        indicator.addPagerData(null, icons.size(), viewpager);
    }

    private void initToolbar() {
        if (type == 2) {//从我的兑换里面进入的
            toolbar.setRightIv(R.drawable.ic_toobar_gray_point);
            getTv.setText("立即领取");
        } else {
            getTv.setText("立即兑换");
        }

        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {
                if (deletePop != null) {
                    //这个东西再7.0 以后会有问题
                    if (Build.VERSION.SDK_INT >= 24) {

                        deletePop.measureHeight();
                        deletePop.showAsDropDown(toolbar.getRightIv(), -ArmsUtils.dip2px(getApplication(), 74), 0, Gravity.LEFT);
                    } else {

                        deletePop.showAsDropDown(toolbar.getRightIv(), -ArmsUtils.dip2px(getApplication(), 74), 0, Gravity.LEFT);
                    }

                }
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @Override
    public GiftDetailsActivity getActivity() {
        return this;
    }


    @Override
    public void popBackSureDelete() {
        showMessage("确认删除");
    }

    @Override
    public void popBackDelete() {
        sureDeletePop.show();
        backgroundAlpha(0.7f);
    }
    @Override
    public void popBackConfirmChange() {
       successChangePop.show();
       backgroundAlpha(0.7f);
    }

    @Override
    public void dismissPop() {
        if (sureDeletePop != null && sureDeletePop.isShowing()) {
            sureDeletePop.dismiss();
        }
        if (deletePop != null && deletePop.isShowing()) {
            deletePop.dismiss();
        }
        if(confirmChangePop!=null&&confirmChangePop.isShowing()){
            confirmChangePop.dismiss();
        }
        if(successChangePop!=null&&successChangePop.isShowing()){
            successChangePop.dismiss();
        }

    }

    @Override
    public int getUserScore() {
        return 200;
    }




    @OnClick({  R.id.get_tv})
    public void onViewClicked(View view) {
        if(DoubleClickUtils.getInstance().isDouble(view)){
            return;
        }
        switch (view.getId()) {

            case R.id.get_tv:
                if(type==2){

                }else {
                    confirmChangePop.show();
                    backgroundAlpha(0.7f);
                }
                break;
                default:
        }
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(1);
    }
}
